﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RPG
{
    //Checkbox with Events, "Apple Style" ;>
    public class CGUICheckBox : CGUIObject
    {
        //Private
        private bool isChecked = false;
        private Color borderColor;
        private Color backgroundColor;
        private Color sliderOnColor;
        private Color sliderOffColor;
        private float borderWidth;
        private float sliderPadding; //Not an absolute value, factor

        //Event Handler
        public CheckBoxEventHandler Event_OnLeftPress;
        public CheckBoxEventHandler Event_OnRightPress;
        public CheckBoxEventHandler Event_OnLeftRelease;
        public CheckBoxEventHandler Event_OnRightRelease;

        //Constructor
        public CGUICheckBox(Vector2 _position, Vector2 _size)
        {
            //Set Aligned Properties
            position = _position;
            size = _size;
            //Set Default Properties
            backgroundColor = Color.Blue;
            borderColor = Color.Yellow;
            sliderOnColor = Color.Green;
            sliderOffColor = Color.Red;
            borderWidth = 0.3f;
            sliderPadding = 0.1f;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Vector2 sliderMin;
            Vector2 sliderMax;
            //Check if the parent is null
            if (parentWindow != null)
            {
                //Draw the Background
                Primitives2D.FillRect(spriteBatch, parentWindow.windowPosition + position,
                    parentWindow.windowPosition + position + size, backgroundColor);
                //Draw the Border
                Primitives2D.DrawRect(spriteBatch, parentWindow.windowPosition + position,
                    parentWindow.windowPosition + position + size, borderWidth, borderColor);
            }
            else
            {
                //Draw the Background
                Primitives2D.FillRect(spriteBatch, position, position + size, backgroundColor);
                //Draw the Border
                Primitives2D.DrawRect(spriteBatch, position, position + size, 1f, borderColor);
            }
            //Draw the Slider
            if(isChecked)
            {
                sliderMin = new Vector2(position.X + size.X * sliderPadding / 2, position.Y + size.Y * sliderPadding);
                sliderMax = new Vector2(position.X + size.X / 2 - size.X * sliderPadding / 2,
                    position.Y + size.Y - size.Y * sliderPadding);
                if(parentWindow != null)
                    Primitives2D.FillRect(spriteBatch, parentWindow.windowPosition + sliderMin, 
                        parentWindow.windowPosition + sliderMax, sliderOnColor);
                else
                    Primitives2D.FillRect(spriteBatch, sliderMin, sliderMax, sliderOnColor);
            }
            else
            {
                sliderMin = new Vector2(position.X + size.X * sliderPadding/2 + size.X/2, position.Y + size.Y * sliderPadding);
                sliderMax = new Vector2(position.X + size.X - size.X * sliderPadding / 2,
                     position.Y + size.Y - size.Y * sliderPadding);
                if (parentWindow != null)
                    Primitives2D.FillRect(spriteBatch, parentWindow.windowPosition + sliderMin,
                        parentWindow.windowPosition + sliderMax, sliderOffColor);
                else
                    Primitives2D.FillRect(spriteBatch, sliderMin, sliderMax, sliderOffColor);
            }
        }

        //Called on update
        public override void Update(GameTime gameTime, MouseState oldMouseState, MouseState newMouseState)
        {
            //Change the Boolean on left click
            if (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
            {
                //Checks if Mouse intersects the button
                Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                if (mouseBoundingBox.Intersects(boundingBox))
                {
                    //On intersection, change
                    isChecked = !isChecked;
                }
            }
            //Check if the Left Click event is not null
            if (Event_OnLeftPress != null)
            {
                //Check if the left mouse button was clicked
                if (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
                {
                    //Checks if Mouse intersects the button
                    Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                    BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                    if (mouseBoundingBox.Intersects(boundingBox))
                    {
                        //On intersection call OnLeftClick
                        Event_OnLeftPress.Invoke(this);
                    }
                }
            }
            //Check if the Right Click event is not null
            if (Event_OnRightPress != null)
            {
                //Same as above with Right Click
                if (newMouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton == ButtonState.Released)
                {
                    Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                    BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                    if (mouseBoundingBox.Intersects(boundingBox))
                    {
                        Event_OnRightPress.Invoke(this);
                    }
                }
            }
            //Check if the Left Release event is not null
            if (Event_OnLeftRelease != null)
            {
                //Check if the Left Mouse Button was released
                if (newMouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed)
                {
                    Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                    BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                    if (mouseBoundingBox.Intersects(boundingBox))
                    {
                        Event_OnLeftRelease.Invoke(this);
                    }
                }
            }
            //Check if the Right Release event is not null
            if (Event_OnRightRelease != null)
            {
                //Check if the Right Mouse Button was released
                if (newMouseState.RightButton == ButtonState.Released && oldMouseState.RightButton == ButtonState.Pressed)
                {
                    Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                    BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                    if (mouseBoundingBox.Intersects(boundingBox))
                    {
                        Event_OnRightRelease.Invoke(this);
                    }
                }
            }
        }

        //Gets
        public bool IsChecked(){ return isChecked; }

        //Sets
        public void SetSliderOnColor(Color whichColor){ sliderOnColor = whichColor; }
        public void SetSliderOffColor(Color whichColor){ sliderOffColor = whichColor; }
        public void SetBackgroundColor(Color whichColor){ backgroundColor = whichColor; }
        public void SetBorderColor(Color whichColor){ borderColor = whichColor; }
        public void SetSize(Vector2 whichSize){ size = whichSize; }
        public void SetPosition(Vector2 whichPosition){ position = whichPosition; }
        public void SetBorderWidth(float whichWidth){ borderWidth = whichWidth; }
        public void SetChecked(bool _isChecked){ isChecked = _isChecked; }
        public void SetSliderPadding(float whichPadding){ sliderPadding = whichPadding; }
    }
}
